package com.starview.platform.data.type;

import java.util.Stack;

import com.starview.star.compiler.type.Dictionary;

public class TypeRewriter extends AbstractTypeTransformer
{
  private final ITypeAlias rule;
  private final Dictionary dict;

  protected TypeRewriter(ITypeAlias rule, Dictionary dict)
  {
    super(new Stack<String>());
    this.rule = rule;
    this.dict = dict;
  }

  @Override
  public IType transformSimpleType(Type t)
  {
    try {
      IType reslt = rule.apply(t, rule.getLoc(), dict);
      if (reslt != null)
        return reslt;
    } catch (TypeConstraintException e) {
    }

    return super.transformSimpleType(t);
  }

  @Override
  public IType transformTypeExp(TypeExp t)
  {
    try {
      IType reslt = rule.apply(t, rule.getLoc(), dict);
      if (reslt != null)
        return reslt;
    } catch (TypeConstraintException e) {
    }
    return super.transformTypeExp(t);
  }
}
